﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Spelprojekt.View
{
    public class Camera
    {        
        public static int g_width = 1280;
        public static int g_height = 720;
        
        public int m_displacementRight = 1216;        

        public float m_scaleX = 64;
        public float m_scaleY = 48;

        public float m_top = 0;
        public float m_left = 0;

        int indentedX = 40;
        int indentedY = 40;
        
        public void DoScroll(Vector2 a_modelPos)
        {
            Vector2 VisualPos = new Vector2(a_modelPos.X * m_scaleX, a_modelPos.Y * m_scaleY);
            m_top = (VisualPos.Y - g_height/2);
            m_left = (VisualPos.X - g_width/2);

            if (m_top > m_scaleY)
            {
                m_top = Model.Map.g_height * m_scaleY - g_height - m_scaleY;
            }

            if (m_top < m_scaleY)
            {
                m_top = Model.Map.g_height * m_scaleY - g_height - m_scaleY;
            } 

            if (m_left > m_displacementRight)
            {
                m_left = Model.Map.g_width * m_scaleX - g_width - m_scaleX;
            }

            if (m_left < m_scaleX)
            {
                m_left = Model.Map.g_width * m_scaleX - g_width*2 + m_scaleX;
            }
        }

        /*        
         * 
         */

        public Vector2 GetTopLeftPercent()
        {
            float totalVisualSizeY = Model.Map.g_height * m_scaleY;
            float totalVisualSizeX = Model.Map.g_width * m_scaleX;

            return new Vector2(-m_left / totalVisualSizeX, -m_top / totalVisualSizeY);
        }
        
        public Vector2 GetViewPos(Vector2 a_modelPos)
        {
            return new Vector2(-m_left + a_modelPos.X * m_scaleX, -m_top + a_modelPos.Y * m_scaleY);
        }

        /*public Vector2 GetViewPosition(Vector2 a_modelPos)
        {
            return new Vector2(-m_left + a_modelPos.X * m_scaleX, -m_top + a_modelPos.Y * m_scaleY);
        }*/

        /*
         * Screen koordinates for pictures related to character
         */

        public Vector2 GetPlayerViewPos(Vector2 a_modelPos)
        {
            return GetViewPos(a_modelPos) - new Vector2(0, 40);
        }

        public Vector2 GetEnemyViewPos(Vector2 a_modelPos)
        {
            return GetViewPos(a_modelPos) - new Vector2(0, 20);
        }

        public Vector2 GetBlockViewPos(Vector2 a_modelPos)
        {
            return GetViewPos(a_modelPos) - new Vector2(0, 31);
        }

        /*
         * Rectanglar
         */
        
        public Rectangle GetViewPos(Vector2 a_modelPos, Vector2 a_scaleSize)
        {
            int x_width = (int)(m_scaleX * a_scaleSize.X);
            int y_width = (int)(m_scaleY * a_scaleSize.Y);

            int x = indentedX + (int)(a_modelPos.X * m_scaleX);
            int y = indentedY + (int)(a_modelPos.Y * m_scaleY);

            return new Rectangle(x, y, x_width, y_width);
        }

        public Rectangle GetViewRectangle(Vector2 a_position, float a_size)
        {
            int width = (int)(m_scaleX * a_size);
            int height = (int)(m_scaleY * a_size);

            int posX = (int)(a_position.X * m_scaleX);
            int posY = (int)(a_position.Y * m_scaleY);

            Rectangle rect = new Rectangle(posX, posY, width, height);

            return rect;
        }

        public Rectangle GetViewRectangle(Vector2 a_position, float a_width, float a_height)
        {
            int width = (int)(m_scaleX * a_width);
            int height = (int)(m_scaleY * a_height);

            int posX = (int)(a_position.X * m_scaleX);
            int posY = (int)(a_position.Y * m_scaleY);

            Rectangle rect = new Rectangle(posX, posY, width, height);

            return rect;
        }

        //return a rectangle with its position at the center
        public Rectangle GetViewRectangleCenter(Vector2 a_position, float a_size)
        {
            Vector2 pos = GetViewPos(a_position);

            int width = (int)(m_scaleX * a_size);
            int height = (int)(m_scaleY * a_size);

            int x = (int)(pos.X - width / 2);
            int y = (int)(pos.Y - height / 2);

            Rectangle rect = new Rectangle(x, y, width, height);

            return rect;
        }

        //returns a rectangle with its position at the center
        public Rectangle GetViewRectangleCenter(Vector2 a_position, float a_width, float a_height)
        {
            int width = (int)(m_scaleX * a_width);
            int height = (int)(m_scaleY * a_height);

            int x = (int)(a_position.X * m_scaleX - width / 2);
            int y = (int)(a_position.Y * m_scaleY - height / 2);

            Rectangle rect = new Rectangle(x, y, width, height);

            return rect;
        }

        public Rectangle GetPlayerViewPos(Vector2 a_modelPos, Vector2 a_playerSize)
        {
            int x_width = (int)(m_scaleX * a_playerSize.X);
            int y_width = (int)(m_scaleY * a_playerSize.Y);

            int x = indentedX + (int)(a_modelPos.X * m_scaleX - x_width);

            int y = indentedY + (int)(a_modelPos.Y * m_scaleY - y_width);

            return new Rectangle(x, y, x_width, y_width);
        }                             

        public Rectangle GetPlayerViewRectangle(Vector2 a_position, float a_size)
        {
            Vector2 pos = GetPlayerViewPos(a_position);

            int width = (int)(m_scaleX * a_size);
            int height = (int)(m_scaleY * a_size);

            int x = (int)(pos.X - width / 2);
            int y = (int)(pos.Y - height / 2);

            Rectangle rect = new Rectangle(x, y, width, height);

            return rect;
        }

        public Rectangle GetEnemyViewRectangle(Vector2 a_position, float a_size)
        {
            Vector2 pos = GetEnemyViewPos(a_position);

            int width = (int)(m_scaleX * a_size);
            int height = (int)(m_scaleY * a_size);

            int x = (int)(pos.X - width / 2);
            int y = (int)(pos.Y - height / 2);

            Rectangle rect = new Rectangle(x, y, width, height);

            return rect;
        }       
    }
}
